<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($apply['id']){
		$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('level','viptime'));
		if($apply['status']==1 && $this->setting['biglevelid'] && $member['level']!=$this->setting['biglevelid']){
			$apply['status']=3;
		}
		$return['apply']=$apply;
	}
	$return['applystatus']=$apply['status'];
	$setting=$this->plugin_setting('page_setting');
	set_html('title',$setting['title']?:'申请成为大客户');
	$return['setting']=$setting;
	$member=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['member']=$member?:false;
	if(!$apply['id']){
		$return['item']=$member;
	}else{
		$return['item']=$apply;
	}
	$setting['diyformfields']=$apply['diyformdata']?json_decode($apply['diyformdata'],true):$setting['diyformfields'];
	$return['item']['diyformfields']=$setting['diyformfields']?:array();
	foreach($return['item']['diyformfields'] as &$item){
		if($item['type']==4 || $item['type']==5){
			$item['value']=str_array($item['value']);
		}
	}
	$return['xieyi']=$this->plugin_setting('xieyi2');
	$return['sharedata']=array(
		'title'=>$setting['title']?:'申请成为大客户',
		'desc'=>$setting['title']?:'申请成为大客户',
		'link'=>custom_url('jy_newczfee/apply//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>tomedia($return['pluginset']['logo']),
	);
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	}
	if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'diyformdata'=>ijson_encode($post['diyformfields']),
	);
	if($post['wechat']){
		$member_update['wechat']=trim($post['wechat']);
	}
	$this->model('member')->apply_agent($_SESSION['uid'],$member_update);
	exi('申请成功！请耐心等待管理员审核！','','/index/');
}